package com.tianzeculture.stagebao.activity.authentication;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.tianzeculture.stagebao.R;
import com.tianzeculture.stagebao.base.BaseActivity;
import com.tianzeculture.stagebao.bean.AuthenticationBean;
import com.tianzeculture.stagebao.net.UserRestUsage;
import com.tianzeculture.stagebao.network.Request;
import com.tianzeculture.stagebao.network.enity.MSG;
import com.tianzeculture.stagebao.network.util.ToastUtil;
import com.tianzeculture.stagebao.util.ImageUtils;
import com.tianzeculture.stagebao.util.Util;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import me.nereo.multi_image_selector.MultiImageSelector;
import me.nereo.multi_image_selector.MultiImageSelectorActivity;

import static com.tianzeculture.stagebao.net.base.AppBaseRestUsageV2.PICTURE_URL;

/**
 * Created by Administrator on 2018/10/15.
 */

public class PersonalAuthenticationActivity extends BaseActivity {
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvSubtitle)
    TextView tvSubtitle;
    @BindView(R.id.ibtnDelete)
    ImageButton ibtnDelete;
    @BindView(R.id.tvRight)
    TextView tvRight;
    @BindView(R.id.tvStatus)
    TextView tvStatus;
    @BindView(R.id.etNumber)
    EditText etNumber;
    @BindView(R.id.etName)
    EditText etName;
    @BindView(R.id.ivFront)
    ImageView ivFront;
    @BindView(R.id.ivBehind)
    ImageView ivBehind;
    @BindView(R.id.btnLogin)
    Button btnLogin;

    private static final int FRONT_PIC = 3;
    private static final int BEHIND_PIC = 4;
    String path1 = "";
    String path2 = "";
    Map<String, String> params = new HashMap<>();
    private int personalAuthenticationId = 5;
    private int getAuthenticationId = 6;
    @Request
    UserRestUsage userRestUsage = null;

    String regex ="\\d{17}[0-9Xx]|\\d{15}";

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_personal_authentication;
    }

    @Override
    public void initUI() {
        super.initUI();
        tvTitle.setText("身份认证");
        if (!"1".equals(Util.AuthenticationStatus)) {
            userRestUsage.getAuthentication(getAuthenticationId, new HashMap<String, String>());
            }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case FRONT_PIC:
                if (resultCode == RESULT_OK) {
                    if (data == null)
                        return;
                    path1 = data.getStringArrayListExtra(MultiImageSelectorActivity.EXTRA_RESULT).get(0);
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            Bitmap bitmap = ImageUtils.compressImage(ImageUtils.getImageBitmap(path1));
                            String base64 = ImageUtils.bitmapToBase64(bitmap);
                            params.put("frontView", base64);
                        }
                    }).start();
                    Glide.with(this).load(path1).skipMemoryCache(true).into(ivFront);
                }
                break;
            case BEHIND_PIC:
                if (resultCode == RESULT_OK) {
                    if (data == null)
                        return;
                    path2 = data.getStringArrayListExtra(MultiImageSelectorActivity.EXTRA_RESULT).get(0);
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            Bitmap bitmap = ImageUtils.compressImage(ImageUtils.getImageBitmap(path2));
                            String base64 = ImageUtils.bitmapToBase64(bitmap);
                            params.put("backView", base64);
                        }
                    }).start();
                    Glide.with(this).load(path2).skipMemoryCache(true).into(ivBehind);
                }
                break;
        }
    }

    @OnClick({R.id.ivFront, R.id.ivBehind, R.id.btnLogin})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ivFront:
                MultiImageSelector.create()
                        .showCamera(true)
                        .single()
                        .start(this, FRONT_PIC);
                break;
            case R.id.ivBehind:
                MultiImageSelector.create()
                        .showCamera(true)
                        .single()
                        .start(this, BEHIND_PIC);
                break;
            case R.id.btnLogin:
                if (TextUtils.isEmpty(etName.getText())) {
                    ToastUtil.showShort(this, "姓名不能为空");
                    return;
                }
                if (TextUtils.isEmpty(etNumber.getText())) {
                    ToastUtil.showShort(this, "身份证号码不能为空");
                    return;
                }
                if (Pattern.matches(regex,etNumber.getText().toString().trim())){
                    ToastUtil.showShort(this,"请输入正确的身份证号");
                    return;
                }
                if ("".equals(path1)) {
                    ToastUtil.showShort(this, "请上传身份证正面");
                    return;
                }
                if ("".equals(path2)) {
                    ToastUtil.showShort(this, "请上传身份证反面");
                    return;
                }
                params.put("name", etName.getText().toString().trim());
                params.put("idNum", etNumber.getText().toString().trim());
                params.put("userId", Util.UserId);
                params.put("status", "2");
                userRestUsage.personalAuthentication(personalAuthenticationId, params);
                break;
        }
    }

    @Override
    public void refreshUI(int taskId, MSG msg) {
        super.refreshUI(taskId, msg);
        if (taskId == personalAuthenticationId) {
            ToastUtil.showShort(this, msg.getMessage());
            if (msg.getData()) {
                Util.AuthenticationStatus = "2";
                finish();
            }
        }
        if (taskId == getAuthenticationId) {
            if (msg.getData()) {
                AuthenticationBean bean = (AuthenticationBean) msg.getObj();
                Util.AuthenticationStatus = bean.getStatus();
                switch (Util.AuthenticationStatus){
                    case "2":
                        etNumber.setEnabled(false);
                        etName.setEnabled(false);
                        ivFront.setEnabled(false);
                        ivBehind.setEnabled(false);
                        btnLogin.setVisibility(View.GONE);
                        tvStatus.setText("待审核");
                        break;
                    case "3":
                        etNumber.setEnabled(false);
                        etName.setEnabled(false);
                        ivFront.setEnabled(false);
                        ivBehind.setEnabled(false);
                        btnLogin.setVisibility(View.GONE);
                        tvStatus.setText("已认证");
                        break;
                    case "4":
                        tvStatus.setText("审核未通过,请确认完信息再重新上传");
                        break;
                }
                etName.setText(bean.getName());
                etNumber.setText(bean.getIdNum());
                Glide.with(this).load(PICTURE_URL + bean.getFrontView()).placeholder(R.mipmap.test).error(R.mipmap.test).into(ivFront);
                Glide.with(this).load(PICTURE_URL + bean.getBackView()).placeholder(R.mipmap.test).error(R.mipmap.test).into(ivBehind);
            }
        }
    }
}
